﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BasicComponents.dialogs
{
    /// <summary>
    /// Interaction logic for Dialog_TreeTable.xaml
    /// </summary>
    public partial class Dialog_TreeTableBMZY : Window
    {
        //保存左边的功能树选中的部门代码
        String bmdm = "";
        //保存表格数据源
        DataTable dt = null;
        //保存部门列名称
        String bmdmColName = "";
        //保存职员代码列名称
        String zydmColName = "";
        //保存职员姓名列名称
        String zyxmColName = "";
        //保存选择后的职员代码
        public String zydm = "";
        //保存先择后的职员姓名
        public String zyxm = "";
        public Dialog_TreeTableBMZY()
        {
            InitializeComponent();
        }
        public Dialog_TreeTableBMZY(DataTable dt_bmxx, DataTable dt_zyxx, String bmdmFjfa, char bmdmSpliterChar, String bmdmColName, String bmdmColHeader, String bmmcColName, String bmmcColHeader, String zydmColName, String zydmColHeader, String zyxmColName, String zyxmColHeader)
        {
            InitializeComponent();
            iniTreeAndTable(dt_bmxx, dt_zyxx, bmdmFjfa, bmdmSpliterChar, bmdmColName, bmdmColHeader, bmmcColName, bmmcColHeader, zydmColName, zydmColHeader, zyxmColName, zyxmColHeader);
        }
        //加载树结构信息
        private void iniTree(String fjfa, char spliterChar, DataTable dt, String dmColName, String mcColName)
        {
            foreach (DataRow row in dt.Rows)
            {
                String dm = row[dmColName].ToString();
                String preDM = DMFJUtils.DMFJUtils.pearentDM(dm, spliterChar, fjfa);
                TreeViewItem tvi_new = new TreeViewItem();
                tvi_new.Name = "_" + row[dmColName].ToString();
                tvi_new.Header = row[dmColName] + "-" + row[mcColName];
                TreeViewItem preNode = null;
                if (preDM == null || preDM.Trim().Equals(""))
                {
                    Boolean isExits = false;
                    foreach (TreeViewItem tvi_ in this.treeView.Items)
                    {
                        if (tvi_.Name.Equals("_" + dm))
                        {
                            isExits = true;
                            break;
                        }
                    }
                    if (!isExits)
                    {
                        this.treeView.Items.Add(tvi_new);
                    }
                }
                else
                {
                    foreach (TreeViewItem tvi_root in this.treeView.Items)
                    {
                        preNode = getPreTreeNode(tvi_root, preDM);
                        if (preNode != null)
                        {
                            Boolean isExits = false;
                            foreach (TreeViewItem tvi_ in preNode.Items)
                            {
                                if (tvi_.Name.Equals("_" + dm))
                                {
                                    isExits = true;
                                    break;
                                }
                            }
                            if (!isExits)
                            {
                                preNode.Items.Add(tvi_new);
                            }
                            break;
                        }

                    }
                    if (preNode == null)
                    {
                        Boolean isExits = false;
                        foreach (TreeViewItem tvi_ in this.treeView.Items)
                        {
                            if (tvi_.Name.Equals("_" + dm))
                            {
                                isExits = true;
                                break;
                            }
                        }
                        if (!isExits)
                        {
                            this.treeView.Items.Add(tvi_new);
                        }
                    }
                }
            }
        }
        //遍历节点信息
        private TreeViewItem getPreTreeNode(TreeViewItem tvi_root, String preDM)
        {
            if (tvi_root.Name.Substring(1).Equals(preDM))
            {
                return tvi_root;
            }
            else if (preDM.IndexOf(tvi_root.Name.Substring(1)) == 0 && tvi_root.HasItems)
            {
                foreach (TreeViewItem tbi in tvi_root.Items)
                {
                    TreeViewItem tvi = getPreTreeNode(tbi, preDM);
                    if (tvi != null)
                    {
                        return tvi;
                    }
                    else
                    {
                        continue;
                    }
                }
                return null;
            }
            else
            {
                return null;
            }
        }
        //设置表格的数据源和列
        public void iniTreeAndTable(DataTable dt_bmxx, DataTable dt_zyxx, String bmdmFjfa, char bmdmSpliterChar, String bmdmColName, String bmdmColHeader, String bmmcColName, String bmmcColHeader, String zydmColName, String zydmColHeader, String zyxmColName, String zyxmColHeader)
        {
            this.bmdmColName = bmdmColName;
            this.zydmColName = zydmColName;
            this.zyxmColName = zyxmColName;
            this.dt = dt_zyxx;
            this.table.ItemsSource = dt.DefaultView;
            //初始化tree
            iniTree(bmdmFjfa, bmdmSpliterChar, dt_bmxx, bmdmColName, bmmcColName);
            //初始化表格列信息
            DataGridTextColumn bmdmColumn = new DataGridTextColumn();
            bmdmColumn.Header = bmdmColHeader;
            bmdmColumn.Binding = new Binding()
            {
                Path = new PropertyPath(bmdmColName)// 需绑定的数据源属性名  
                ,
                Mode = BindingMode.TwoWay// 绑定模式  
                ,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            this.table.Columns.Add(bmdmColumn);
            DataGridTextColumn bmmcColumn = new DataGridTextColumn();
            bmmcColumn.Header = bmmcColHeader;
            bmmcColumn.Binding = new Binding()
            {
                Path = new PropertyPath(bmmcColName)// 需绑定的数据源属性名  
                ,
                Mode = BindingMode.TwoWay// 绑定模式  
                ,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            this.table.Columns.Add(bmmcColumn);
            DataGridTextColumn zydmColumn = new DataGridTextColumn();
            zydmColumn.Header = zydmColHeader;
            zydmColumn.Binding = new Binding()
            {
                Path = new PropertyPath(zydmColName)// 需绑定的数据源属性名  
                ,
                Mode = BindingMode.TwoWay// 绑定模式  
                ,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            this.table.Columns.Add(zydmColumn);
            DataGridTextColumn zyxmColumn = new DataGridTextColumn();
            zyxmColumn.Header = zyxmColHeader;
            zyxmColumn.Binding = new Binding()
            {
                Path = new PropertyPath(zyxmColName)// 需绑定的数据源属性名  
                ,
                Mode = BindingMode.TwoWay// 绑定模式  
                ,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            this.table.Columns.Add(zyxmColumn);
        }
        //树结构选择变化事件
        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            if (this.treeView.SelectedItem != null)
            {
                DataTable dt = this.dt.Copy();
                String dm = (this.treeView.SelectedItem as TreeViewItem).Name.Substring(1);
                this.bmdm = dm;
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count;)
                    {
                        if (!(dt.Rows[i][this.bmdmColName].ToString().IndexOf(dm) >= 0))
                        {
                            dt.Rows.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                    this.table.ItemsSource = null;
                    this.table.ItemsSource = dt.DefaultView;
                }
            }
        }
        //双击表格事件
        private void table_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.table.SelectedItem != null)
            {
                this.zydm = (this.table.SelectedItem as DataRowView).Row[this.zydmColName].ToString();
                this.zyxm = (this.table.SelectedItem as DataRowView).Row[this.zyxmColName].ToString();
                this.DialogResult = true;
                this.Close();
            }
        }
        //设置搜索的关键字
        public void setKeyWorks(String keyWords)
        {
            this.uc_keywords.TxtValue = keyWords;
            btn_search_Click(btn_search, null);
        }
        //选择变化事件
        private void table_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.table.SelectedItem != null)
            {
                this.zydm = (this.table.SelectedItem as DataRowView).Row[this.zydmColName].ToString();
                this.zyxm = (this.table.SelectedItem as DataRowView).Row[this.zyxmColName].ToString();
            }
        }
        //确定按钮事件
        private void btn_selected_Click(object sender, RoutedEventArgs e)
        {
            if (this.table.SelectedItem != null)
            {
                this.zydm = (this.table.SelectedItem as DataRowView).Row[this.zydmColName].ToString();
                this.zyxm = (this.table.SelectedItem as DataRowView).Row[this.zyxmColName].ToString();
                this.DialogResult = true;
                this.Close();
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.zydm = "";
            this.zyxm = "";
            this.DialogResult = false;
            this.Close();

        }

        private void UC_LabelTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter))
            {
                btn_search_Click(sender, e);
            }
        }

        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String keyWords = uc_keywords.TxtValue;
                if (keyWords == null || uc_keywords.Equals(""))
                {
                    DataTable dt = this.dt.Copy();
                    String dm = this.bmdm;
                    if (dt != null)
                    {
                        for (int i = 0; i < dt.Rows.Count;)
                        {
                            if (dm != null && !dm.Equals("") && !(dt.Rows[i][this.bmdmColName].ToString().IndexOf(dm) >= 0))
                            {
                                dt.Rows.RemoveAt(i);
                            }
                            else
                            {
                                i++;
                            }
                        }
                        this.table.ItemsSource = null;
                        this.table.ItemsSource = dt.DefaultView;
                    }
                }
                else
                {
                    searchKeyWords(keyWords, this.dt);
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show("筛选失败，错误信息：" + e1.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //筛选关键字事件
        private void searchKeyWords(String key, DataTable dt)
        {

            DataTable dt_new = dt.Copy();
            if (dt_new != null)
            {
                for (int i = 0; i < dt_new.Rows.Count;)
                {
                    if (
                        !((dt_new.Rows[i][this.zydmColName].ToString() + "[" + dt_new.Rows[i][this.zyxmColName].ToString()+"]").IndexOf(key) >= 0)  &&
                        !((dt_new.Rows[i][this.zydmColName].ToString() + "-" + dt_new.Rows[i][this.zyxmColName].ToString()).IndexOf(key) >= 0) && !((dt_new.Rows[i][this.zydmColName].ToString() + dt_new.Rows[i][this.zyxmColName].ToString()).IndexOf(key) >= 0) && !(NPinyin.Pinyin.GetInitials(dt_new.Rows[i][this.zyxmColName].ToString()).ToUpper().IndexOf(key.ToUpper()) >= 0) && !(NPinyin.Pinyin.GetInitials(dt_new.Rows[i][this.zyxmColName].ToString()).ToUpper().IndexOf(key.ToUpper()) >= 0))
                    {
                        dt_new.Rows.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
                this.table.ItemsSource = null;
                this.table.ItemsSource = dt_new.DefaultView;
            }
        }
        //
        private void btn_CancelSearch_Click(object sender, RoutedEventArgs e)
        {
            this.uc_keywords.TxtValue = "";
            btn_search_Click(btn_search, null);
        }


    }
}
